package com.atguigu.crowd.mvc.handler;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.atguigu.crowd.entity.Admin;
import com.atguigu.crowd.entity.ParamData;
import com.atguigu.crowd.entity.Quyuejiaophoto0;
import com.atguigu.crowd.entity.Student;
import com.atguigu.crowd.service.api.AdminService;
import com.atguigu.crowd.service.api.QuyuejiaoPhotoService;
import com.atguigu.crowd.util.CrowdUtil;
import com.atguigu.crowd.util.ResultEntity;

@Controller
public class TestHandler {
	
	@Autowired
	private AdminService adminService;
	
	@Autowired
	private QuyuejiaoPhotoService quyuejiaoPhotoService;
	
	@RequestMapping("test/ssm.ht")
	public String testSsm(ModelMap modelMap,HttpServletRequest request)  {
		
		List<Admin> adminList= adminService.getAll();
		
		modelMap.addAttribute("adminList", adminList);
		
		return "target";
	}
	
	@ResponseBody
	@RequestMapping("send/array/one.ht")
	public String testReceiveArrayOne(@RequestParam("array[]") List<Integer> array) {
		
		for (Integer integer : array) {
			System.out.println("number="+integer);
		}
		
		return "success1";
	}
	
	@ResponseBody
	@RequestMapping("send/array/two.ht")
	public String testReceiveArrayTwo(ParamData paramData) {
		
		List<Integer> ListArray=paramData.getArray();
		
		for (Integer integer : ListArray) {
			System.out.println("number="+integer);
		}
		
		return "success2";
	}
	
	@ResponseBody
	@RequestMapping("send/array/three.ht")
	public String testReceiveArrayThree(@RequestBody List<Integer> array) {
		
		Logger logger=LoggerFactory.getLogger(TestHandler.class);
		
		for (Integer integer : array) {
			System.out.println("number="+integer);
			logger.info("哈哈哈哈");
		}
		
		return "success3";
	}
	
	@ResponseBody
	@RequestMapping("send/compose/object.ht")
	/**
	 * 表述层-Ajax-发送复杂对象(看看@RequestBody有多强大)
	 * @return
	 */
	public String testReceiveComposeObject(@RequestBody Student student) {
		
		LoggerFactory.getLogger(TestHandler.class).info(student.toString());
		System.out.println("-------------------------------------------------");
		System.out.println(student.toString());
		
		return "successObject(复杂对象)";
	}
	
	//-----------------------------------------------------------------------
	//51-表述层-Ajax-ResultEntity-代码-为了方便前端工程师和后端工程师无缝对接
	@ResponseBody
	@RequestMapping("test/5/send/compose/objectPowerful.json")
	/**
	 * 表述层-Ajax-发送复杂对象(看看@RequestBody有多强大)
	 * @return
	 */
	public ResultEntity<Student> testReceiveComposeObjectResultEntity(@RequestBody Student student) {
		
		//将"查询"到的Student 对象封装到ResultEntity中返回
		ResultEntity<Student> resultEntity=ResultEntity.successWithData(student);
		
		System.out.println(student.toString());
		LoggerFactory.getLogger(TestHandler.class).info(student.toString());
		
		return resultEntity;
		
	}
	//01_05环境搭建-异常映射
	//测试一下异常类
	@RequestMapping("test/ssmException.ht")
	public String testSsmException(ModelMap modelMap,HttpServletRequest request)  {
		
		List<Admin> adminList= adminService.getAll();
		
		modelMap.addAttribute("adminList", adminList);
		
		System.out.println(10/0);
		
		return "target";
	}
	
	//01_05环境搭建-异常映射_测试 CrowdUtil类（判断是普通请求还是ajax请求）
	@RequestMapping("test/ssmCrowdUtil.ht")
	public String testSsmCrowdUtil(ModelMap modelMap,HttpServletRequest request) {
		
		boolean judgeResult=CrowdUtil.judgeRequestType(request);
		
		LoggerFactory.getLogger(getClass()).info("judgeResult = = "+judgeResult);
		System.out.println("-----------------------------------------------");
		System.out.println("judgeResult = "+judgeResult);
		
		List<Admin> adminList=adminService.getAll();
		
		modelMap.addAttribute("adminList", adminList);
		
		return "target";
	}

	//01_05环境搭建-异常映射_测试 基于注解的异常映射
	@RequestMapping("test/ssmCrowdExceptionResolver.json")
	public String testSsmCrowdExceptionResolver(ModelMap modelMap,HttpServletRequest request) {
		
		boolean judgeResult=CrowdUtil.judgeRequestType(request);
		
		LoggerFactory.getLogger(getClass()).info("judgeResult = = "+judgeResult);
		System.out.println("-----------------------------------------------");
		System.out.println("judgeResult = "+judgeResult);
		
		List<Admin> adminList=adminService.getAll();
		
		modelMap.addAttribute("adminList", adminList);
		
		//造一个空指针异常
		String a=null;
		System.out.println(a.length());
		
		return "target";
	}
	@ResponseBody
	@RequestMapping("test/ssmCrowdExceptionMath.ht")
	public String testMath( ) {
		
		double result= 8/0;
		System.out.println(result);
		
		return "target";
	}
	@ResponseBody
	@RequestMapping("test8/ssmCrowdExceptionMath.ht")
	public String test8Math() {
		
		double result= 8/0;
		System.out.println(result);
		
		return "target";
	}

	@RequestMapping("test/ssmCrowdExceptionMath2.ht")
	public String testMath2(ModelMap modelMap,HttpServletRequest request)  {
		
		List<Admin> adminList= adminService.getAll();
		
		modelMap.addAttribute("adminList", adminList);
		
		String test=null;
		System.out.println(test.length());
		
		return "target";
	}
	
/*	@ResponseBody*/
	@RequestMapping("test/ssmCrowdExceptionMath3.ht")
	public String testMath3(ModelMap modelMap,HttpServletRequest request)  {
		
		
/*		String test=null;
		System.out.println(test.length());*/
		
		return "target";
	}
	
	//@ResponseBody
	@RequestMapping("/test/quyuejiao0.ht")
	public String testQuyuejiao0(ModelMap modelMap,HttpServletRequest request) {
		
		List<Quyuejiaophoto0> quyuejiaoPhotoList= quyuejiaoPhotoService.getAll();
		
		modelMap.addAttribute("quyuejiaoPhotoList", quyuejiaoPhotoList);
		
		//返回的是页面（json页面）,因为我们没有加 @ResponseBody
		return "quyuejiaoWeb";
	}
	@ResponseBody
	@RequestMapping("/test/quyuejiao1.ht")
	public List<Quyuejiaophoto0> testQuyuejiao1() {
		
		List<Quyuejiaophoto0> quyuejiaoPhotoList= quyuejiaoPhotoService.getAll();
		
		System.out.println(quyuejiaoPhotoList);
		
		//返回的是json数据 ,因为我们加了 @ResponseBody
		return quyuejiaoPhotoList;
	}

	//51Pic-表述层-前后端分离测试
	@ResponseBody
	@RequestMapping("test/Pic/send/compose/objectPowerful.json")

	public String testReceiveComposeObjectResultEntityPic(@RequestBody String Index) {
		
		//将"查询"到的Student 对象封装到ResultEntity中返回
		
		System.out.println("------------------"+Index.toString());
		LoggerFactory.getLogger(TestHandler.class).info(Index.toString());
		
		return Index;
		
	}
	
	@ResponseBody
	@RequestMapping("/test/ajax/async.ht")
	public String testAsync() {
		
		return "success";
	}
	
	//测试代码
	@Test
	public void test01() {
		
		int i=1;
		int j;
		if(i++==1&&++i==2){ //i=2
			j=1;
		}else {
			j=2;
			}
			System. out. println("i = " + i) ;
			System. out . println("j ="+j) ;

		}
	//测试代码
	@Test
	public void test02() {
		
		  char[] letters=new char[26];
		  char[]newletters=new char[26];
		  for(int i=0;i<letters.length;i++){
		   System.out.println((char)('A'+i));
		  }
		  for(int i=0;i<letters.length;i++){
		   newletters[i]=letters[letters.length-i-1];
		  }
		  letters=newletters;
		  System.out.println("********************************");
		  for(int i=0;i<newletters.length;i++){
		   System.out.println(newletters[i]);
		  }
	}
	
}























